创建UnityWebRequests

WebRequests可以像任何其他对象一样实例化。有两个构造函数可用:

    • 标准的无参数构造函数创建一个新的UnityWebRequest,其中所有设置为空白或默认。目标网址未设置,未设置自定义标题,并且重定向限制设置为32。
    • 第二个构造函数接受一个字符串参数。它将UnityWebRequest的目标URL分配给字符串参数的值,否则与无参数构造函数相同。

多个其他属性可用于设置,跟踪状态和检查结果或UnityWebRequest

        UnityWebRequest wr = new UnityWebRequest(); // Completely blank
        UnityWebRequest wr2 = new UnityWebRequest("http://www.mysite.com"); // Target URL is set

        // the following two are required to web requests to work
        wr.url = "http://www.mysite.com";
        wr.method = UnityWebRequest.kHttpVerbGET;   // can be set to any custom method, common constants privided

        wr.useHttpContinue = false;
        wr.chunkedTransfer = false;
        wr.redirectLimit = 0;  // disable redirects
        wr.timeout = 60;       // don't make this small, web requests do take some time

🔚